﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYView;

namespace YJKArch.DimensionModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdDoorWindowDim : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)11;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (AssistFunc.GetCurrentViewLevel(cmdData.Application.ActiveUIDocument.Document) == null)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			WindowHandle windowHandle = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			DlgDoorWindowDim dlgDoorWindowDim = new DlgDoorWindowDim();
			dlgDoorWindowDim.Show(windowHandle);
			int wMsg = 7;
			CmdDoorWindowDim.SendMessage(windowHandle.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			Transaction transaction = new Transaction(cmdData.Application.ActiveUIDocument.Document, "DoorWindowDim");
			transaction.Start();
			try
			{
				DimensionCreator dimensionCreator = new DimensionCreator(cmdData.Application);
				while (dimensionCreator.DoorWindowDim(dlgDoorWindowDim) != Result.Cancelled)
				{
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			transaction.Commit();
			dlgDoorWindowDim.Close();
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
